package ${project.packageName}.${project.moduleName}.infrastructure.dal.repository;

import com.github.pagehelper.PageInfo;
import ${project.packageName}.${project.moduleName}.domain.music.${tableStructure.classNameUppercase}Entity;
import ${project.packageName}.${project.moduleName}.domain.music.repository.${tableStructure.classNameUppercase}Repository;
import ${project.packageName}.${project.moduleName}.domain.music.vo.${tableStructure.classNameUppercase}QueryVO;
import ${project.packageName}.${project.moduleName}.infrastructure.dal.assembler.I${tableStructure.classNameUppercase}Assembler;
import ${project.packageName}.${project.moduleName}.infrastructure.dal.mapper.${tableStructure.classNameUppercase}Mapper;
import ${project.packageName}.${project.moduleName}.infrastructure.dal.model.${tableStructure.classNameUppercase}Model;
import com.rabbitpre.tubit.core.page.PageData;
import com.rabbitpre.tubit.core.page.PageParam;
import com.rabbitpre.tubit.orm.core.PageHelperBuilder;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * ${tableStructure.tableComment}存储Impl
 *
 * @author ${developer.author}
 * @date ${developer.datetime}
 */
@Repository
public class ${tableStructure.classNameUppercase}RepositoryImpl implements ${tableStructure.classNameUppercase}Repository {

    @Resource
    private ${tableStructure.classNameUppercase}Mapper ${tableStructure.className}Mapper;


    @Override
    public boolean add${tableStructure.classNameUppercase}(${tableStructure.classNameUppercase}Entity entity) {
        ${tableStructure.classNameUppercase}Model ${tableStructure.className}Model = I${tableStructure.classNameUppercase}Assembler.IMPL.doToModel(entity);
        return ${tableStructure.className}Mapper.insert(${tableStructure.className}Model) > 0;
    }

    @Override
    public boolean delete${tableStructure.classNameUppercase}ById(Long ${tableStructure.className}Id) {
        /**
         * 不使用deleteByPrimaryKey,因为不会去解析@LogicDelete逻辑删除标记
         */
        Example example = new Example(${tableStructure.classNameUppercase}Model.class);
        Example.Criteria ${tableStructure.className}Criteria = example.createCriteria();
        ${tableStructure.className}Criteria.andEqualTo("id",${tableStructure.className}Id);
        return ${tableStructure.className}Mapper.deleteByExample(example) > 0;
    }

    @Override
    public boolean update${tableStructure.classNameUppercase}ById(${tableStructure.classNameUppercase}Entity entity) {
        ${tableStructure.classNameUppercase}Model ${tableStructure.className}Model = I${tableStructure.classNameUppercase}Assembler.IMPL.doToModel(entity);
        return ${tableStructure.className}Mapper.updateByPrimaryKey(${tableStructure.className}Model) > 0;
    }

    @Override
    public ${tableStructure.classNameUppercase}Entity selectById(Long ${tableStructure.className}Id) {
        ${tableStructure.classNameUppercase}Model ${tableStructure.className}Model = ${tableStructure.className}Mapper.selectByPrimaryKey(${tableStructure.className}Id);
        return I${tableStructure.classNameUppercase}Assembler.IMPL.modelToDo(${tableStructure.className}Model);
    }

    @Override
    public PageData<${tableStructure.classNameUppercase}Entity> queryPageList(${tableStructure.classNameUppercase}QueryVO ${tableStructure.className}QueryVO, PageParam pageParam) {

        ${tableStructure.classNameUppercase}Model ${tableStructure.className}Model = I${tableStructure.classNameUppercase}Assembler.IMPL.voToModel(${tableStructure.className}QueryVO);

        //构建分页处理
        PageHelperBuilder.startPage(pageParam);
        Example example = new Example(${tableStructure.classNameUppercase}Model.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtils.isNotBlank(${tableStructure.className}Model.getName())) {
            criteria.andLike("name", "%"+${tableStructure.className}Model.getName()+"%");
        }

        //执行查询操作
        List<${tableStructure.classNameUppercase}Model> ${tableStructure.className}ModelList = ${tableStructure.className}Mapper.selectByExample(example);

        //转换对象
        PageInfo pageInfo = new PageInfo(${tableStructure.className}ModelList);
        List<${tableStructure.classNameUppercase}Entity> userEntities = I${tableStructure.classNameUppercase}Assembler.IMPL.modelListToDoList(${tableStructure.className}ModelList);
        //转换对象之后，塞回原来的pageinfo对象
        pageInfo.setList(userEntities);
        //使用hepler构造pagedata对象
        PageData<${tableStructure.classNameUppercase}Entity> pageData = PageHelperBuilder.createPageData(pageInfo);
        return pageData;
    }
}
